﻿using DevComponents.DotNetBar.SuperGrid;
using SGUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0244C : Form
    {

        private string sql = "";
        string _VoucherID = "";

        public frm0244C()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHOME"] as frmHOME).CloseTab(this);
        }

        void loadDATA_Combo()
        {
            // Kỳ kế toán
            sql = "SELECT DISTINCT";
            sql += " MonthYear , TranMonth , TranYear FROM vi0317 WHERE DivisionID = '" + FRM.DivisionID + "' AND CHARINDEX('" + FRM.ID_Group + "',GroupShow)>0";
            sql += " ORDER BY TranYear DESC, TranMonth DESC";
            DataTable dt_Period = FRM.GetDataTable(sql);
            TDBCboFromPeriod.DataSource = dt_Period;
            TDBCboFromPeriod.SelectedValue = FRM.Period;
            TDBCboToPeriod.DataSource = dt_Period;
            TDBCboToPeriod.SelectedValue = FRM.Period;

            DTPFromDate.Value = FRM.DateDue;
            DTPToDate.Value = FRM.DateDue;
            GUI.ChangeDateFormat(DTPFromDate, FRM.SystemDateFormat);
            GUI.ChangeDateFormat(DTPToDate, FRM.SystemDateFormat);
        }

        void loadDATA_Grid()
        {
            if (TDBCboFromPeriod.SelectedItem == null || TDBCboToPeriod.SelectedItem == null) { return; }

            sql = "Select tb0191.* , ObjectName, IsUpdateName From tb0191 Left Join tb0045 On tb0045.ObjectID = tb0191.ObjectID  ";
            sql += string.Format(" Where TransactionTypeID='T33' And tb0191.DivisionID = '{0}' ", FRM.DivisionID);
            if (optFromPeriod.Checked)
            {
                sql += " AND TranMonth+TranYear*100 BETWEEN " + (int.Parse(TDBCboFromPeriod.SelectedItem.ItemData[1]) + int.Parse(TDBCboFromPeriod.SelectedItem.ItemData[2]) * 100).ToString();
                sql += " AND " + (int.Parse(TDBCboToPeriod.SelectedItem.ItemData[1]) + int.Parse(TDBCboToPeriod.SelectedItem.ItemData[2]) * 100).ToString();
            }
            else
            {
                sql += " AND VoucherDate BETWEEN '" + DTPFromDate.Value.ToShortDateString() + "' AND '" + DTPToDate.Value.ToShortDateString() + "'";
            }
            sql += " Order by VoucherTypeID,VoucherDate, VoucherNo";
            DataTable dt_Grid1 = FRM.GetDataTable(sql);
            //DFORMAT.NumberFormat(ref dt_Grid1, new string[] { "OriginalAmount", "ConvertedAmount", "TaxAmount" }, '.');
            DBRender.GridDB(GridDB1, dt_Grid1);
            Application.DoEvents();
            GridDB1.Update();
            GridDB1.PrimaryGrid.Footer.Text = String.Format("Tổng số dòng: {0:#,#}", GridDB1.PrimaryGrid.Rows.Count);
        }

        private void frm0244B_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
            optFromPeriod.Checked = true;
        }
        private void frm0244B_Load(object sender, EventArgs e)
        {

        }

        private void TDBCboFromPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (optFromPeriod.Checked) { loadDATA_Grid(); }
        }

        private void TDBCboToPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (optFromPeriod.Checked) { loadDATA_Grid(); }
        }

        private void optFromPeriod_CheckedChanged(object sender, EventArgs e)
        {
            loadDATA_Grid();
        }

        private void DTPFromDate_ValueChanged(object sender, EventArgs e)
        {
            if (optFromDate.Checked) { loadDATA_Grid(); }
        }

        private void DTPToDate_ValueChanged(object sender, EventArgs e)
        {
            if (optFromDate.Checked) { loadDATA_Grid(); }
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            loadDATA_Grid();
        }

        private void GridDB1_SelectionChanged(object sender, DevComponents.DotNetBar.SuperGrid.GridEventArgs e)
        {
            GridRow gr = (GridRow)e.GridPanel.ActiveRow;
            if (gr == null)
            {
                _VoucherID = "";
                return;
            }
            _VoucherID = gr["clVoucherID"].Value.ToString();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            string tn = "tb0191";
            string ws = string.Format("WHERE VoucherID='{0}'", _VoucherID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

    }
}
